<template>
  <a-drawer
    :title="title"
    :width="width"
    placement="right"
    :closable="false"
    @close="close"
    :visible="visible">
  
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入任务名称"></a-input>
        </a-form-item>
        <a-form-item label="任务开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择任务开始时间" v-decorator="[ 'startTime', validatorRules.startTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="任务结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择任务结束时间" v-decorator="[ 'endTime', validatorRules.endTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="任务类型，1每天轮回，到上线时段上线，下线时间到下线，2开始时间上线，结束时间时下线" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'schedulerType', validatorRules.schedulerType]" placeholder="请输入任务类型，1每天轮回，到上线时段上线，下线时间到下线，2开始时间上线，结束时间时下线" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="onlineHour" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'onlineHour', validatorRules.onlineHour]" placeholder="请输入onlineHour" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="offlineHour" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'offlineHour', validatorRules.offlineHour]" placeholder="请输入offlineHour" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="onlineTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择onlineTime" v-decorator="[ 'onlineTime', validatorRules.onlineTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="offlineTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择offlineTime" v-decorator="[ 'offlineTime', validatorRules.offlineTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="createTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择createTime" v-decorator="[ 'createTime', validatorRules.createTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="updateTime" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择updateTime" v-decorator="[ 'updateTime', validatorRules.updateTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="createBy" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'createBy', validatorRules.createBy]" placeholder="请输入createBy" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="updateBy" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'updateBy', validatorRules.updateBy]" placeholder="请输入updateBy" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="deleted" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'deleted', validatorRules.deleted]" placeholder="请输入deleted" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="status" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'status', validatorRules.status]" placeholder="请输入status" style="width: 100%"/>
        </a-form-item>
        
      </a-form>
    </a-spin>
    <a-button type="primary" @click="handleOk">确定</a-button>
    <a-button type="primary" @click="handleCancel">取消</a-button>
  </a-drawer>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  
  export default {
    name: "ProductOnlineTaskModal",
    components: { 
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        name:{rules: [{ required: true, message: '请输入任务名称!' }]},
        startTime:{},
        endTime:{},
        schedulerType:{rules: [{ required: true, message: '请输入任务类型，1每天轮回，到上线时段上线，下线时间到下线，2开始时间上线，结束时间时下线!' }]},
        onlineHour:{rules: [{ required: true, message: '请输入onlineHour!' }]},
        offlineHour:{rules: [{ required: true, message: '请输入offlineHour!' }]},
        onlineTime:{rules: [{ required: true, message: '请输入onlineTime!' }]},
        offlineTime:{rules: [{ required: true, message: '请输入offlineTime!' }]},
        createTime:{},
        updateTime:{},
        createBy:{},
        updateBy:{},
        deleted:{rules: [{ required: true, message: '请输入deleted!' }]},
        status:{rules: [{ required: true, message: '请输入status!' }]},
        },
        url: {
          add: "/product/productOnlineTask/add",
          edit: "/product/productOnlineTask/edit",
        }
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','startTime','endTime','schedulerType','onlineHour','offlineHour','onlineTime','offlineTime','createTime','updateTime','createBy','updateBy','deleted','status'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'name','startTime','endTime','schedulerType','onlineHour','offlineHour','onlineTime','offlineTime','createTime','updateTime','createBy','updateBy','deleted','status'))
      }
      
    }
  }
</script>

<style lang="less" scoped>
/** Button按钮间距 */
  .ant-btn {
    margin-left: 30px;
    margin-bottom: 30px;
    float: right;
  }
</style>